<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class S extends CI_Controller{

    public  $data ;
    function __construct() {
        parent::__construct();
    }

    public function index()
    {
        $is_login = $this->session->userdata('tool_login');
        if(!$is_login)  redirect(site_url('s/login'));
        $this->data['tool_account'] = $this->session->userdata('tool_account');
        $this->data['unset_url'] = site_url('s/unset_login');
        $this->load->view('s/index',$this->data);
    }

    public function login()
    {
       if(strtolower($_SERVER['REQUEST_METHOD'])=='post')
       {
              $account = $this->input->get_post('account');
              $password = $this->input->get_post('password');
              $response = $this->user_data($account,$password);

           if($response['status'])
           {
               $this->session->sess_expiration = '3600';
               $this->session->set_userdata('tool_login',1);
               $this->session->set_userdata('tool_account',$account);
               //$this->success($response['msg'],site_url('s/index'));
               echo '<script>alert("'.$response['msg'].'")</script>';
               redirect(site_url('s/index'));
           }
           else
           {
               echo '<script>alert("'.$response['msg'].'")</script>';
             //  $this->error($response['msg'],'#');
           }
       }
        $this->data['login_url'] = site_url('s/login');
        $this->load->view('s/login',$this->data);
    }

    public function unset_login()
    {
        $this->session->unset_userdata('tool_login');
        $this->session->unset_userdata('tool_account');
        redirect(site_url('s/login'));
    }

    public function user_data($account,$paddword)
    {
        $user_data = array('101001'=>'sm567');
        if(!empty($user_data[$account]))
        {
            if($paddword==$user_data[$account])
            {
                return array('status'=>'1','msg'=>'登录成功');
            }
            else
            {
                return array('status'=>'0','msg'=>'密码错误');
            }
        }
        else
        {
            return array('status'=>'0','msg'=>'无效帐号');
        }

    }




    public function success($info='',$url='')
    {
        echo json_encode(array('info'=>$info,'status'=>'1','url'=>$url));
        exit;
    }


    public function error($info,$url)
    {
        echo json_encode(array('info'=>$info,'status'=>'0','url'=>$url));
        exit;
    }

}